use axum::Json;
use com::{ chrono, file::create::create_dir_all, process, query};
use sea_orm::query::*;
// 备份表
pub(super) async fn backup_table(Json(body):Json<JsonValue>) -> Result<Json<JsonValue>, Json<JsonValue>>{
    let table_name = query::vec_str(&body,"table_name")?;
    let pwd = query::str(&body,"pwd")?;
    let path = query::str(&body,"path")?;
    let t = query::usize_op(&body,"type")?;
    let group = vec!["", " -t", " -d"];
    let name = query::str(&body,"name")?;
    let dbname = query::str(&body,"database")?;
    let table_name = table_name.join(" ");
    let order = format!(
        "mysqldump -u root -p{} {} {} {} > {}", 
        pwd, 
        dbname, 
        table_name,
        if let Some(f) = t { group.get(f).unwrap() } else {""},
        path + "/" + &name + "_" + &chrono::now_format(3) + ".sql" 
    );
    process::cmd_spawn(&order).await
}
// 备份数据库
pub(super) async fn backup_database(Json(body):Json<JsonValue>) -> Result<Json<JsonValue>, Json<JsonValue>>{
    let pwd = query::str(&body,"pwd")?;
    let path = query::str(&body,"path")?;
    let t = query::usize_op(&body,"type")?;
    let group = vec!["", " -t", " -d"];
    let name = query::str(&body,"name")?;
    let dbname = query::vec_str(&body,"database")?;
    let dbname = dbname.join(" ");
    let order = format!(
        "mysqldump -u root -p{} {} {} > {}", 
        pwd, 
        dbname, 
        if let Some(f) = t { group.get(f).unwrap() } else {""},
        path + "/" + &name + "_" + &chrono::now_format(3) + ".sql" 
    );
    process::cmd_spawn(&order).await
}
// 导入 SQL
pub(super) async fn import_table_sql(Json(body):Json<JsonValue>) -> Result<Json<JsonValue>, Json<JsonValue>>{
    // let pwd = query::str(&body,"pwd")?;
    let path = query::str(&body,"path")?;
    let dbname = query::str(&body,"database")?;
    let order = format!(
        "mysqldump -u root -p {} < {}", 
        // pwd, 
        dbname, 
        path
    );
    process::cmd_spawn(&order).await
}
// 备份 mongodb
pub(super) async fn backup_mongo(Json(body):Json<JsonValue>) -> Result<Json<JsonValue>, Json<JsonValue>>{
    // let pwd = query::str(&body,"pwd")?;
    let path = query::str(&body,"path")?;
    let dbname = query::str(&body,"database")?;
    let name = query::str(&body,"name")?;
    let username = query::str(&body,"username")?;
    let pwd = query::str(&body,"pwd")?;
    let folder = path + "/" + &name + "_" + &chrono::now_format(3);
    let folder = folder.replace("\\", "/");

    _ = create_dir_all(&folder).await?;

    let order = format!(
        "mongodump --host localhost --port 27017 --username {} --password '{}' --authenticationDatabase {} --out {}", 
        username,
        pwd,
        dbname,
        folder
    );
    process::cmd_spawn(&order).await
}
pub(super) async fn backup_all_mongo(Json(body):Json<JsonValue>) -> Result<Json<JsonValue>, Json<JsonValue>>{
    // let pwd = query::str(&body,"pwd")?;
    let path = query::str(&body,"path")?;
    let name = query::str(&body,"name")?;
    let pwd = query::str(&body,"pwd")?;
    let folder = path + "/" + &name + "_" + &chrono::now_format(3);
    let folder = folder.replace("\\", "/");
    let dev = query::bool(&body, "dev")?;

    _ = create_dir_all(&folder).await?;

    let order = if dev {
        format!(
            "mongodump --host localhost --port 27017 --out {}", 
            folder
        )
    }else{
        format!(
            "mongodump --host localhost --port 27017 --username root --password '{}' --out {}", 
            pwd,
            folder
        )
    };
    process::cmd_spawn(&order).await
}